# 1.server 例子
# from socket import *
# import time
# ip_port=('localhost',51590)
# bank_log=5
# buffer_size=1024
# tcp_server=socket(AF_INET,SOCK_STREAM)
# tcp_server.bind(ip_port)
# tcp_server.listen(bank_log)
# print('waiting for connection...')
# conn,addr = tcp_server.accept()
# print("双向链接是",conn)
# print("客户端地址",addr)
# while True:
#     data=conn.recv(buffer_size)
#     print('server===>')
#     print("客户端发来的消息是",data.decode("utf-8"))
#     time.sleep(5)
#     conn.sendall(data.upper())
#
# conn.close()
# tcp_server.close()

# ------------TCP套叠字--------------------
import socket,time

ip_port=('localhost',51590)
bank_log=5
buffer_size=1024
tcp_server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# sk.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) # 重用ip和端口解决time——wait状态（address already in use）
tcp_server.bind(ip_port)
tcp_server.listen(bank_log)
print("waiting for connet...")
while True:
    print("server working...")
    conn,addr = tcp_server.accept()
    print('server===>')
    print("双向链接是",conn)
    print("客户端地址",addr)
    while True:
        try:
            data=conn.recv(buffer_size)
            print("clent send message is :",data.decode("utf-8"))
            time.sleep(2)
            conn.sendall(data.upper())
        except Exception :
            print(Exception)
            break
    conn.close()
sk.close()
